﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using digitwhu.Models.IRepositorys;
using digitwhu.Models;
using digitwhu.Models.Repositorys;
using digitwhu.Helper;
using digitwhu.Logic.ClassLibrary;
using digitwhu.Logic.BusinessLogic;

namespace digitwhu.Logic.BusinessLogicControl
{
    public class UserLogicControl
    {
        #region 用户
        public static string AddUser(string userName, string password, string email)//用户注册
        {
            //userName:用户名，即nickname，暂时可以相同
            //password:密码
            //email:注册邮箱，不可相同，登录凭邮箱验证
            //返回值：-1表示邮箱已经存在，-2表示注册失败(未能在表User中创建记录)，正数表示注册成功(返回的是用户的唯一编号)
            string userId = UserLogic.AddUser(userName, password, email).ToString();
            return userId;
        }
        public static string GetUserLogin(string email, string password)//用户登录
        {
            //email:登录的邮箱地址
            //password:用户密码
            //返回值：用户信息或者null
            int i = UserLogic.GetUserLogin(email, password);
            //返回值说明：正数表示登陆成功(返回用户ID),-1表示密码错误，-2表示用户名不存在
            if (i < 0) return null;
            RUserInfo  u = GetUserInfo(i);
            string result = JsonHelper.JsonSerializer<RUserInfo >(u);
            return result;
        }
        public static string GetUserInfo(string userId)//获取用户信息
        {
            //useId:用户编号
            //返回值：用户信息
            int userid = Int32.Parse(userId);
            RUserInfo  u = GetUserInfo(userid);
            string result = JsonHelper.JsonSerializer<RUserInfo >(u);
            return result;
        }
        public static void UpdateUserInfo(string userId, string realName, string studentId, string school,
           string birthday, string homeAddress, string avatar, string dormId,string nickName)//更新用户信息
        {
            int userid = Int32.Parse(userId);
            decimal studentid;
            try
            {
                studentid = Decimal.Parse(studentId);
            }
            catch (Exception e)
            {
                studentid = -1;
            }
            DateTime birth;
           try{
                birth=DateTime.Parse(birthday);
            }catch(Exception e)
            {
                birth = DateTime.Parse("1-1-1");
            }
            int dormid;
           try{
                dormid = Int32.Parse(dormId);
            }
            catch{
                dormid = -1;
            }

            bool b = UserLogic.UpdateUserInfo(userid, realName, studentid, school, birth, homeAddress, avatar, dormid, nickName);
            //b==false表示更新信息失败，未处理
        }
        public static string UpdateUserPassword(string oldPassword, string newPassword, string userId)//用户更改密码
        {
            //oldPassword:登录的邮箱地址
            //newPassword:用户新密码
            //返回值：-1(用户Id非法)，-2原先密码错误，1表示操作成功
            int userid = Int32.Parse(userId);
            int r = UserLogic.UpdateUserPassword(oldPassword, newPassword, userid);
            return r.ToString();
        }
        public static string GetUserIsInUserAttention(string userPay, string userBe)//查询用户userPay是不是已经关注了用户userBe
        {
            //userPay:主动查询用户编号
            //userBe:被查询用户编号
            //返回值：true:用户userPay关注了用户userBe,false:true:用户userPay没有关注用户userBe
            int userpay = Int32.Parse(userPay);
            int userbe = Int32.Parse(userBe);
            return UserLogic.GetUserIsInUserAttention(userpay, userbe).ToString();
        }
        public static string GetUserInInUserBlackList(string userMake, string userBe)//查询用户userMake是不是已经拉黑了用户userBe
        {
            //userPay:主动查询用户编号
            //userBe:被查询用户编号
            //返回值：true:用户userMake已经拉黑了用户userBe,false:true:用户userMake没有拉黑用户userBe
            int usermake = Int32.Parse(userMake);
            int userbe = Int32.Parse(userBe);
            return UserLogic.GetUserInInUserBlackList(usermake, userbe).ToString();
        }
        #endregion
        #region 公用函数
        public static RUserInfo  GetUserInfo(int userId)
        {
            //用户usreId应该是存在的
            RUserInfo u = new RUserInfo();
            var dbContext = new DigitwhuDataContext();
            u.userInfo = dbContext.UserInfo.Single(c => c.userId == userId);
            User tmp = dbContext.User.Single(b => b.id == userId);
            u.emailAddress = tmp.emailAddress;
            u.lastLoginTime = tmp.lastLoginTime.ToString();
            u.nickName = tmp.nickName.ToString();
            u.userId = tmp.id;
            if (u.userInfo.dormId != null)
            {
                //如果寝室编号不为空
                var name = from b in dbContext.building
                           where b.id == u.userInfo.dormId
                           select b.name;
                u.dormName = name.First<string>();    //获取公寓名字
            }
            else {
                u.dormName = "";
            }
            return u;
        }//获取指定用户的用户信息
        #endregion
    }
}